//
//  YiDaRegisterViewController.swift
//  YiDa
//
//  Created by zhoufei on 16/6/26.
//  Copyright © 2016年 ZhouFei. All rights reserved.
//

import UIKit

typealias registerSuccessBlock = ()->Void
class YiDaRegisterViewController: UIViewController {
    @IBOutlet weak var codeBtn: UIButton!
    @IBOutlet weak var tel: UITextField!
    @IBOutlet weak var code: UITextField!
    @IBOutlet weak var inviter: UITextField!
    @IBOutlet weak var password: UITextField!
    var index:Int = 60
    var codeStr:String = "1088"
    var registerSuccess:registerSuccessBlock?
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    @IBAction func checkCode(sender: AnyObject) {
        let timer:NSTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(YiDaRegisterViewController.time), userInfo: nil, repeats: true)
//        let timer:NSTimer = NSTimer(timeInterval: 1.0, target: self, selector: #selector(YiDaRegisterViewController.time), userInfo: nil, repeats: true)
        timer.fire()
    }
    
    func time(){
        print("111")
        if index > 0{
            
            self.codeBtn.backgroundColor = UIColor.grayColor()
            self.codeBtn.setTitle("(\(String(index))s)获取验证码", forState: .Normal)
            index -= 1
        }
        
    }
    @IBAction func submit(sender: AnyObject) {
        self.view.endEditing(true)
        if PublicFunction.checkByRegex(TEL_PATTEN, text: self.tel.text!){
            if self.code.text == self.codeStr{
                if self.password.text?.characters.count >= 6{
                    if self.inviter.text?.characters.count > 0 {
                        //注册
                        let param:Dictionary<String,AnyObject> = ["tel":self.tel.text!,"password":self.password.text!,"inviter":self.inviter.text!]
                        print(param)
                        YiDaNet.main.request(REGISTER, param: param, completionHandler: { (inner) in
                            do{
                                let result = try inner()
                                if result["error_code"]?.intValue == 200 {
                                    if self.registerSuccess != nil{
                                        self.registerSuccess!()
                                    }
                                    self.dismissViewControllerAnimated(true, completion: nil)
                                }else{
                                    self.view.makeToast(String(result["error_msg"]))
                                }

                            }catch let error{
                                print(error)
                            }
                        })
                    }else{
                        self.view.makeToast("请输入邀请码")
                    }
                }else{
                    self.view.makeToast("密码长度不能小于6位")
                }
            }else{
                self.view.makeToast("验证码输入有误")
            }
        }else{
            self.view.makeToast("电话号码输入有误")
        }
    }
    @IBAction func dismass(sender: AnyObject) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
   
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
